<template>
  <div class="ygmsg">
    <el-form
      :model="form"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号：" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名：" prop="name">
        <el-input v-model="form.name" placeholder="请输入员工姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号：" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="性别：">
        <el-radio-group v-model="form.sex">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号：" prop="idNumber">
        <el-input
          v-model="form.idNumber"
          placeholder="请输入身份证号"
        ></el-input>
      </el-form-item>
      <div class="divider"></div>
      <div class="button-container">
        <el-button @click="quxiao">取消</el-button>
        <el-button type="primary" @click="baocun">保存</el-button>
        <el-button
          style="
            background-color: #000000;
            color: #ffffff;
            border-color: #000000;
          "
          @click="baocuntianjia"
          >保存并继续添加</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { createEmployee, getEmployee, changeStauts } from "@/api/employee";
export default {
  data() {
    const validateZhanghao = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };

    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号"));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };

    const validateIDCard = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入身份证号"));
      } else if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
        callback(new Error("身份证号格式错误"));
      } else {
        callback();
      }
    };
    return {
      form: {
        username: "",
        name: "",
        phone: "",
        sex: "1",
        idNumber: "",
      },
      rules: {
        username: [{ validator: validateZhanghao, trigger: "blur" }],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [{ validator: validatePhone, trigger: "blur" }],
        idNumber: [{ validator: validateIDCard, trigger: "blur" }],
      },
    };
  },
  created() {
    if (this.isEdit) {
      getEmployee(this.id).then((res) => {
        this.form = res.data;
      });
    }
  },
  computed: {
    isEdit() {
      return this.$route.query.isEdit === "1";
    },
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    quxiao() {
      this.$router.push("/employee");
    },
    async baocun() {
      if (this.isEdit) {
        await changeStauts(this.form);
      } else {
        await createEmployee(this.form);
      }
      this.$message.success("操作成功");
      this.$router.push("/employee");
    },
    baocuntianjia() {
      createEmployee(this.form).then(() => {
        this.form = {};
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.ygmsg {
  padding-top: 50px;
  padding-left: 120px;
}
.el-form-item {
  width: 400px;
}
.divider {
  height: 1px;
  background-color: #e4e7ed; /* 浅灰色分隔线 */
  margin: 20px 0; /* 上下间距 */
  width: 1200px; /* 与表单项同宽 */
}
.button-container {
  display: flex;
  gap: 12px; /* 按钮间距 */
  // justify-content: center;
  padding-left: 300px;
  padding-top: 15px;
}
</style>